package com.passive.lock.service.user.imp;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.passive.lock.mapper.TDeptMapper;
import com.passive.lock.mapper.TDeptMapperCustom;
import com.passive.lock.mapper.TRegionMapper;
import com.passive.lock.mapper.TRouteMapper;
import com.passive.lock.mapper.TUserMapper;
import com.passive.lock.model.po.TDept;
import com.passive.lock.model.po.TDeptExample;
import com.passive.lock.model.po.TRegion;
import com.passive.lock.model.po.TRoute;
import com.passive.lock.model.po.TRouteExample;
import com.passive.lock.model.vo.MapTreeVo;
import com.passive.lock.model.vo.TDeptVo;
import com.passive.lock.service.user.DeptService;
import com.passive.lock.utils.DozerMapperUtils;
import com.passive.lock.utils.Result;
import com.passive.lock.utils.tree.TreeDeptUtil;

@Service("deptService")
public class DeptServiceImpl implements DeptService {
	@Resource(name = "TUserMapper")
	private TUserMapper userMapper;
	@Resource(name = "TDeptMapper")
	private TDeptMapper deptMapper;
	@Resource(name = "TDeptMapperCustom")
	private TDeptMapperCustom deptMapperCustom;

	@Resource(name = "TRouteMapper")
	private TRouteMapper routeMapper;

	@Resource(name = "TRegionMapper")
	private TRegionMapper regionMapper;

	@Override
	public Result addDept(TDeptVo tDeptVo) {

		TDept tDept = new TDept();
		DozerMapperUtils.map(tDeptVo, tDept);
		String maxid = deptMapperCustom.maxId(tDeptVo.getPid());
		if (maxid == null) {
			if (tDeptVo.getPid() == null) {
				maxid = "10";
			} else {
				maxid = tDeptVo.getPid() + "10";
			}

		} else {
			maxid = Integer.toString((Integer.parseInt(maxid) + 1));
		}
		tDept.setId(maxid);
		tDept.setState("1");
		TDept d = deptMapper.selectByPrimaryKey(tDeptVo.getPid());
		tDept.setLayer(d.getLayer() + 1);
		tDept.setCreatetime(new Date());
		deptMapper.insert(tDept);
		return Result.ok();

	}

	@Override
	public Result addCompany(TDeptVo tDeptVo) {
		TDept tDept = new TDept();
		tDeptVo.setRegionid(Integer.parseInt(tDeptVo.getArename()));
		DozerMapperUtils.map(tDeptVo, tDept);
		String maxid = deptMapperCustom.maxId(tDeptVo.getPid());
		if (maxid == null) {
			if (tDeptVo.getPid() == null || "".equals(tDeptVo.getPid()) || "0".equals(tDeptVo.getPid())) {
				maxid = "10";
			} else {
				maxid = tDeptVo.getPid() + "10";
			}

		} else {
			maxid = Integer.toString((Integer.parseInt(maxid) + 1));
		}
		tDept.setId(maxid);
		tDept.setState("1");
		TDept d = deptMapper.selectByPrimaryKey(tDeptVo.getPid());
		tDept.setLayer(d.getLayer() + 1);
		tDept.setCreatetime(new Date());
		deptMapper.insert(tDept);
		return Result.ok();
	}

	@Override
	public List<TDeptVo> deptList(String groupid, TDeptVo tDeptVo, Integer offset, Integer limit) {

		List<TDept> list = deptMapperCustom.deptList(groupid, tDeptVo, offset, limit);
		List<TDeptVo> listvo = new ArrayList<TDeptVo>();
		for (TDept tDept : list) {
			TDeptVo vo = new TDeptVo();
			DozerMapperUtils.map(tDept, vo);
			if ("1".equals(vo.getType())) {
				TRegion tRegion1 = regionMapper.selectByPrimaryKey(vo.getRegionid());
				vo.setArename(tRegion1.getName());
				TRegion tRegion2 = regionMapper.selectByPrimaryKey(tRegion1.getParentid());
				vo.setCityname(tRegion2.getName());
				TRegion tRegion3 = regionMapper.selectByPrimaryKey(tRegion2.getParentid());
				vo.setProname(tRegion3.getName());
			}
			listvo.add(vo);
		}
		return listvo;

	}

	@Override
	public int deptListCount(String groupid, TDeptVo tDeptVo) {
		try {
			int count = deptMapperCustom.deptListCount(groupid, tDeptVo);
			return count;
		} catch (Exception e) {
			return 0;
		}
	}

	@Override
	public Result updateDept(TDeptVo tDeptVo) {
		try {
			TDept tDept = new TDept();
			DozerMapperUtils.map(tDeptVo, tDept);
			deptMapper.updateByPrimaryKey(tDept);
			return Result.ok();
		} catch (Exception e) {
			return Result.build(500, "请联系系统管理员");
		}
	}

	@Override
	public Result companyTree(String companyId) {
		TDept tDept = deptMapper.selectByPrimaryKey(companyId);
		List<TDept> list = deptMapperCustom.companyTree(companyId);
		List<TDeptVo> listTree = new ArrayList<TDeptVo>();
		for (TDept tree : list) {
			TDeptVo TDeptVo = new TDeptVo();
			DozerMapperUtils.map(tree, TDeptVo);
			listTree.add(TDeptVo);
		}
		TDept depttree = tDept;
		for (int i = 0; i < tDept.getLayer(); i++) {
			TDept d = deptMapper.selectByPrimaryKey(depttree.getPid());
			TDeptVo TDeptVo = new TDeptVo();
			DozerMapperUtils.map(d, TDeptVo);
			TDeptVo.setIsopen(true);
			listTree.add(TDeptVo);
			depttree = d;
		}
		List<TDeptVo> newTreeDataList = TreeDeptUtil.getfatherNode(listTree);
		return Result.ok(newTreeDataList);
	}

	@Override
	public Result groupTree(String deptId) {

		TDept tDept = deptMapper.selectByPrimaryKey(deptId);
		List<TDept> list = deptMapperCustom.groupTree(deptId);
		List<TDeptVo> listTree = new ArrayList<TDeptVo>();
		for (TDept tree : list) {
			TDeptVo TDeptVo = new TDeptVo();
			DozerMapperUtils.map(tree, TDeptVo);
			listTree.add(TDeptVo);
		}
		TDept depttree = tDept;
		for (int i = 0; i < tDept.getLayer(); i++) {
			TDept d = deptMapper.selectByPrimaryKey(depttree.getPid());
			TDeptVo TDeptVo = new TDeptVo();
			DozerMapperUtils.map(d, TDeptVo);
			TDeptVo.setIsopen(true);
			listTree.add(TDeptVo);
			depttree = d;
		}
		List<TDeptVo> newTreeDataList = TreeDeptUtil.getfatherNode(listTree);
		return Result.ok(newTreeDataList);
	}

	@Override
	public Result deptTree(String companyId) {
		List<TDept> list = deptMapperCustom.deptTree(companyId);
		List<TDeptVo> listTree = new ArrayList<TDeptVo>();
		for (TDept tree : list) {
			TDeptVo TDeptVo = new TDeptVo();
			DozerMapperUtils.map(tree, TDeptVo);
			listTree.add(TDeptVo);
		}
		List<TDeptVo> newTreeDataList = TreeDeptUtil.getfatherNode(listTree, companyId);
		return Result.ok(newTreeDataList);
	}

	@Override
	public Result deleteDept(TDeptVo tDeptVo) {
		int count = deptMapper.deleteByPrimaryKey(tDeptVo.getId());
		return Result.ok(count);
	}

	@Override
	public Result deleteDeptList(List<String> ids) {
		int counts = 0;
		for (String id : ids) {
			deptMapper.deleteByPrimaryKey(id);
			counts++;
		}
		return Result.ok(counts);
	}

	@Override
	public Result queryCompany(String id) {
		TDept tdept = deptMapper.selectByPrimaryKey(id);
		return Result.ok(tdept);
	}

	@Override
	public Result queryMapTree1(String pid) {
		TDept dept = deptMapper.selectByPrimaryKey(pid);
		MapTreeVo vo = new MapTreeVo();
		vo.setId(dept.getId());
		vo.setLayer(dept.getLayer());
		vo.setName(dept.getFullname());
		vo.setPid(dept.getPid());
		List<MapTreeVo> listvo = new ArrayList<>();
		listvo.add(vo);
		return Result.ok(listvo);
	}

	@Override
	public Result queryMapTree2(String pid) {
		TDeptExample example1 = new TDeptExample();
		TDeptExample.Criteria criteria1 = example1.createCriteria();
		criteria1.andPidEqualTo(pid);
		List<TDept> list1 = deptMapper.selectByExample(example1);

		TRouteExample example2 = new TRouteExample();
		TRouteExample.Criteria criteria2 = example2.createCriteria();
		criteria2.andGroupidEqualTo(pid);
		List<TRoute> list2 = routeMapper.selectByExample(example2);

		List<MapTreeVo> listvo = new ArrayList<>();
		for (TDept dept : list1) {
			MapTreeVo vo = new MapTreeVo();
			vo.setId(dept.getId());
			vo.setLayer(dept.getLayer());
			vo.setName(dept.getFullname());
			vo.setPid(dept.getPid());
			listvo.add(vo);
		}

		/*
		 * for (TRoute route : list2) { MapTreeVo vo = new MapTreeVo();
		 * vo.setId(route.getRouteid().toString());
		 * vo.setLayer(dept.getLayer()); vo.setName(route.getRoutename());
		 * vo.setPid(route.getGroupid()); listvo.add(vo); }
		 */

		return Result.ok(listvo);
	}

	@Override
	public Result groupTreeById(String groupid) {

		List<TDept> list = deptMapperCustom.groupTree(groupid);
		List<TDeptVo> listTree = new ArrayList<>();
		for (TDept tree : list) {
			TDeptVo TDeptVo = new TDeptVo();
			DozerMapperUtils.map(tree, TDeptVo);
			listTree.add(TDeptVo);
		}
		List<TDeptVo> newTreeDataList = TreeDeptUtil.getfatherNode(listTree,groupid);
		return Result.ok(newTreeDataList);
	}

}
